package com.chinaunicom.product.trigger.http;

import com.chinaunicom.common.constants.Constants;
import com.chinaunicom.common.result.JsonResult;
import com.chinaunicom.product.domain.model.entity.ProductSkuEntity;
import com.chinaunicom.product.domain.model.query.ProductSpuQuery;
import com.chinaunicom.product.domain.service.ProductService;
import com.chinaunicom.product.trigger.http.vo.ProductQueryVO;
import com.chinaunicom.product.trigger.http.vo.ProductSkuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author guomenghuai
 * @date 2024/1/10
 * @Description
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/productList")
    public JsonResult<List<ProductSkuEntity>> productList() {
        ProductSpuQuery query = ProductSpuQuery.builder().status(Constants.ENABLED).build();
        return JsonResult.success(productService.queryProductList(query));
    }

    @PostMapping("/productDetail")
    public JsonResult<ProductSkuEntity> productDetail(@RequestBody @Valid ProductQueryVO queryVO) {
        ProductSpuQuery query = ProductSpuQuery.builder().productId(queryVO.getProductId()).status(Constants.ENABLED).build();
        return JsonResult.success(productService.queryProductDetail(query));
    }

    @PostMapping("/saveProduct")
    public JsonResult<Boolean> saveProduct(@RequestBody @Valid ProductSkuVO productSkuVO) {
        ProductSkuEntity productSkuEntity = ProductSkuEntity.builder()
                .productType(productSkuVO.getProductType())
                .productName(productSkuVO.getProductName())
                .coverImg(productSkuVO.getCoverImg())
                .currentPrice(productSkuVO.getCurrentPrice())
                .originalPrice(productSkuVO.getOriginalPrice())
                .stock(productSkuVO.getStock())
                .description(productSkuVO.getDescription())
                .status(productSkuVO.getStatus())
                .sellerId(productSkuVO.getSellerId())
                .build();

        return JsonResult.success(productService.saveProduct(productSkuEntity));
    }

}
